package net.suncaper.demo.service;

import net.suncaper.demo.domain.Order;
import net.suncaper.demo.domain.OrderExample;
import net.suncaper.demo.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderMapper orderMapper;
    ShopcartService shopcartService;
    UserService userService;

    @Override
    public List<Order> findorderbyUid(int userId) {
//        OrderExample orderExample = new OrderExample();
//        if(userID != 0){
//            orderExample.createCriteria().andOrderIdEqualTo(userID);
//        }
//        return orderMapper.selectByExample(orderExample);
        OrderExample orderExample = new OrderExample();
        if(userId != 0 ){
            orderExample.createCriteria().andUserIdEqualTo(userId);
        }
        return orderMapper.selectByExample(orderExample);
    }

    //YWY写的，根据商品ID查找订单
    @Override
    public List<Order> findorderbyProduct(int productId) {
        OrderExample orderExample = new OrderExample();
        if(productId !=0){
            orderExample.createCriteria().andProductIdEqualTo(productId);
        }
        return orderMapper.selectByExample(orderExample);
    }

    @Override
    public List<Order> findOrder(){ //显示订单列表

        return orderMapper.selectByExample(new OrderExample());
    }

    @Override
    public void saveUpdateOrderMes(Order order) {//保存修改

        if(order.getOrderId()==null||order.getOrderId().equals("")){
            System.out.println("ERROR");
            System.out.println("修改订单信息错误！");
        }else{
            orderMapper.updateByPrimaryKey(order);
            System.out.println("修改定单信息成功！");
        }

    }

    @Override
    public void delOrderBySIdMes(int orderId) {//删除
        OrderExample orderExample=new OrderExample();
        orderExample.createCriteria().andOrderIdEqualTo(orderId);
        orderMapper.deleteByExample(orderExample);
    }

    @Override
    public void editOrderMes(Order order) {
        OrderExample orderExample = new OrderExample();
    }

    @Override
    public Order findOrderByOId(int orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }


//    @Override
//    public List<Order> findOrderByUId(int userId){//通过user_id查找订单记录
//        OrderExample orderExample = new OrderExample();
//        if(userId != 0 ){
//            orderExample.createCriteria().andUserIdEqualTo(userId);
//        }
//        return orderMapper.selectByExample(orderExample);
//    }

    @Override
    public void saveAddOrderMes(Order order) {//增加
        orderMapper.insert(order);
    }

}
